package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.WriteScenic;
import com.selfdriving.dialog.ChooseDialog;
import com.selfdriving.dialog.ChooseDialog.OnBirthdayDialogClickListener;
import com.selfdriving.dialog.ChooseDialog.OnDialogClickListener;
import com.selfdriving.dialog.EntityListDialogAdapter;
import com.selfdriving.dialog.SimpleListDialogAdapter;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.wl.activity.SimpleListDialog;
import com.selfdriving.wl.activity.SimpleListDialog.onSimpleListItemClickListener;

public class StartTravel implements OnClickListener
{
	/** 当前View **/
	View view;
	BaseApplication application;
	Context context;

	LayoutInflater inflater;
	LinearLayout StartLayou1;
	LinearLayout StartLayou2;
	LinearLayout StartLayou3;
	LinearLayout StartLayou4;
	LinearLayout StartLayou5;
	LinearLayout StartLayou6;
	LinearLayout StartLayou7;
	LinearLayout StartLayou8;
	LinearLayout StartLayou9;
	LinearLayout StartLayou10;
	LinearLayout StartLayou11;
	LinearLayout StartLayou12;
	LinearLayout StartLayou13;
	SimpleListDialog setTimeDialog;
	ChooseDialog dialog;
	String sexArray[] = { "不限", "男", "女" };
	// String days[] = { "不限", "1天", "3天", "7天", "10天", "30天", "1月以上", "3月以上" };
	String times[] = { "不限", "三日内", "一周内", "一月内" };
	String ages[] = { "不限", "16-20岁", "23-30岁", "31-40岁", "40岁以上" };
	List<Entity> togetherWay = new ArrayList<Entity>();
	List<Entity> carList = new ArrayList<Entity>();
	Button seach;
	// //////////////
	String togetherWayId;
	String sex;
	String days;
	String car;

	TextView destination;// 目的地
	TextView begin;// 开始时间
	TextView end;// 结束时间
	TextView goDate;// 出发时间
	TextView from;// 出发地点
	TextView declaration;// 宣言
	TextView summary;// 详情
	TextView num;// 人数
	TextView fee;// 人均费用

	public StartTravel(BaseApplication application, Context context,
			Button seach)
	{
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.activity_together_start_travel, null);
		this.application = application;
		this.context = context;
		this.seach = seach;
		initViews();
		initEvents();
		getTogetherWayList();
	}

	public void getTogetherWayList()
	{
		togetherWay.clear();
		togetherWay.add(new WriteScenic("不限", "9"));
		togetherWay.add(new WriteScenic("单对单", "0"));
		togetherWay.add(new WriteScenic("情人结伴", "1"));
		togetherWay.add(new WriteScenic("家庭结伴", "2"));
		togetherWay.add(new WriteScenic("多对多", "3"));
		carList.clear();
		carList.add(new WriteScenic("无车", "0"));
		carList.add(new WriteScenic("有车", "1"));
	}

	public void initViews()
	{
		StartLayou1 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but1);
		StartLayou2 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but2);
		StartLayou3 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but3);
		StartLayou4 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but4);
		StartLayou5 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but5);
		StartLayou6 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but6);
		StartLayou7 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but7);
		StartLayou8 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but8);
		StartLayou9 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but9);
		StartLayou10 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but10);
		StartLayou11 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but11);
		StartLayou12 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but12);
		StartLayou13 = (LinearLayout) view
				.findViewById(R.id.together_start_travel_but13);
		destination = (TextView) view
				.findViewById(R.id.together_start_travel_text1);
		begin = (TextView) view.findViewById(R.id.together_start_travel_text2);
		end = (TextView) view.findViewById(R.id.together_start_travel_text3);
		goDate = (TextView) view.findViewById(R.id.together_start_travel_text4);
		from = (TextView) view.findViewById(R.id.together_start_travel_text5);
		declaration = (TextView) view
				.findViewById(R.id.together_start_travel_text6);
		summary = (TextView) view
				.findViewById(R.id.together_start_travel_text7);
		num = (TextView) view.findViewById(R.id.together_start_travel_text8);
		fee = (TextView) view.findViewById(R.id.together_start_travel_text9);
	}

	public void initEvents()
	{
		StartLayou1.setOnClickListener(this);
		StartLayou2.setOnClickListener(this);
		StartLayou3.setOnClickListener(this);
		StartLayou4.setOnClickListener(this);
		StartLayou5.setOnClickListener(this);
		StartLayou6.setOnClickListener(this);
		StartLayou7.setOnClickListener(this);
		StartLayou8.setOnClickListener(this);
		StartLayou9.setOnClickListener(this);
		StartLayou10.setOnClickListener(this);
		StartLayou11.setOnClickListener(this);
		StartLayou12.setOnClickListener(this);
		StartLayou13.setOnClickListener(this);
		seach.setOnClickListener(this);
	}

	public View getView()
	{
		return view;
	}

	public void setText(View v)
	{
		LinearLayout layout = (LinearLayout) v;
		final TextView text = (TextView) layout.getChildAt(1);
		TextView text0 = (TextView) layout.getChildAt(0);
		final String title = text0.getText().toString();
		dialog = new ChooseDialog(context, title, text.getText().toString());
		dialog.setOnDialogClickListener(new OnDialogClickListener()
		{
			@Override
			public void OnOkClick(String st)
			{
				if (st != null && !st.equals(""))
				{
					text.setText(st);
				}
				dialog.dismiss();
			}

			@Override
			public void OnCancelClick()
			{
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	public void setDate(View v)
	{
		LinearLayout layout = (LinearLayout) v;
		final TextView text = (TextView) layout.getChildAt(1);
		TextView text0 = (TextView) layout.getChildAt(0);
		String title = text0.getText().toString();
		dialog = new ChooseDialog(application, context, "设置日期");
		dialog.setTitle(title);
		dialog.setOnBirthdayDialogClickListener(new OnBirthdayDialogClickListener()
		{
			@Override
			public void OnOkClick(String st)
			{
				if (st != null && !st.equals(""))
				{
					text.setText(st);
				}
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.together_start_travel_but1:
			setText(v);
			break;
		case R.id.together_start_travel_but2:
			setDate(v);
			break;
		case R.id.together_start_travel_but3:
			setDate(v);
			break;
		case R.id.together_start_travel_but4:
			setDate(v);
			break;
		case R.id.together_start_travel_but5:
			setTimeDialog = new SimpleListDialog(context);
			setTimeDialog.setTitle("选择结性别");
			setTimeDialog.setTitleLineVisibility(View.GONE);
			setTimeDialog.setAdapter(new SimpleListDialogAdapter(context,
					sexArray));
			setTimeDialog
					.setOnSimpleListItemClickListener(new OnSimpleListItemClickListener(
							5));
			setTimeDialog.show();
			break;
		case R.id.together_start_travel_but6:
			setTimeDialog = new SimpleListDialog(context);
			setTimeDialog.setTitle("选择结行程天数");
			setTimeDialog.setTitleLineVisibility(View.GONE);
			setTimeDialog
					.setAdapter(new SimpleListDialogAdapter(context, times));
			setTimeDialog
					.setOnSimpleListItemClickListener(new OnSimpleListItemClickListener(
							6));
			setTimeDialog.show();
			break;
		case R.id.together_start_travel_but7:
			setTimeDialog = new SimpleListDialog(context);
			setTimeDialog.setTitle("选择是否有车");
			setTimeDialog.setTitleLineVisibility(View.GONE);
			setTimeDialog.setAdapter(new EntityListDialogAdapter(application,
					context, carList));
			setTimeDialog
					.setOnSimpleListItemClickListener(new OnSimpleListItemClickListener(
							7));
			setTimeDialog.show();
			break;
		case R.id.together_start_travel_but8:
			setTimeDialog = new SimpleListDialog(context);
			setTimeDialog.setTitle("选择要发布到的景点");
			setTimeDialog.setTitleLineVisibility(View.GONE);
			setTimeDialog.setAdapter(new EntityListDialogAdapter(application,
					context, togetherWay));
			setTimeDialog
					.setOnSimpleListItemClickListener(new OnSimpleListItemClickListener(
							8));
			setTimeDialog.show();
			break;
		case R.id.together_start_travel_but9:
			setText(v);
			break;
		case R.id.together_start_travel_but10:
			setText(v);
			break;
		case R.id.together_start_travel_but11:
			setText(v);
			break;
		case R.id.together_start_travel_but12:
			setText(v);
			break;
		case R.id.together_start_travel_but13:
			setText(v);
			break;
		case R.id.top_right_but:
			if (application.user != null)
			{
				release();
			} else
			{
				Utils.showShortToast(context, "您还未登录");
				Intent intent = new Intent(context, RegisterActivity.class);
				context.startActivity(intent);
			}
			break;
		}
	}

	private class OnSimpleListItemClickListener implements
			onSimpleListItemClickListener
	{
		int x = 0;

		public OnSimpleListItemClickListener(int x)
		{
			this.x = x;
		}

		@Override
		public void onItemClick(int position)
		{
			TextView text;
			WriteScenic scenic;
			String st;
			switch (x)
			{
			case 5:
				text = (TextView) StartLayou5.getChildAt(1);
				text.setText(sexArray[position]);
				sex = sexArray[position];
				break;
			case 6:
				text = (TextView) StartLayou6.getChildAt(1);
				text.setText(times[position]);
				days = times[position];
				break;
			case 7:
				text = (TextView) StartLayou7.getChildAt(1);
				scenic = (WriteScenic) carList.get(position);
				st = scenic.getName();
				text.setText(st);
				car = scenic.getId();
				break;
			case 8:
				text = (TextView) StartLayou8.getChildAt(1);
				scenic = (WriteScenic) togetherWay.get(position);
				st = scenic.getName();
				text.setText(st);
				togetherWayId = scenic.getId();
				break;
			}
			setTimeDialog.dismiss();
		}
	}

	public void release()
	{
		String des = destination.getText().toString();
		if (des.length() < 1)
		{
			Utils.showShortToast(context, "请输入目的地");
			return;
		}
		Utils.showLoadingDialog(context, "结伴游发布中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "48");
		params.put("phone", application.user.getPhone());
		params.put("img", application.user.getIcon());
		params.put("name", application.user.getName());
		params.put("begin", begin.getText().toString());
		params.put("end", end.getText().toString());
		params.put("goDate", goDate.getText().toString());
		params.put("destination", destination.getText().toString());
		params.put("sex", sex);
		params.put("days", days);
		params.put("car", car);
		params.put("goType", togetherWayId);
		params.put("fee", fee.getText().toString());
		params.put("from", from.getText().toString());
		params.put("declaration", declaration.getText().toString());
		params.put("summary", summary.getText().toString());
		params.put("num", num.getText().toString());
		System.out.println(params);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.showShortToast(context, "发布失败");
				Utils.dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				if (json.contains("result:000000"))
				{
					Utils.showShortToast(context, "发布成功");
				}
				Utils.dismissLoadingDialog();
			}
		});
	}
}
